package com.atguigu.userprofile.train

import java.util.Properties

import com.atguigu.userprofile.pipeline.MyPipeline
import com.atguigu.userprofile.util.MyPropertiesUtil
import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object StudentGenderTrain {

  def main(args: Array[String]): Unit = {
    //0 环境
    println()
    val sparkConf: SparkConf = new SparkConf().setAppName("task_student_gender_train_app").setMaster("local[*]")
    val sparkSession: SparkSession = SparkSession.builder().config(sparkConf).enableHiveSupport().getOrCreate()


    // 1  查询数据 ==> dataframe
    println("1  查询数据 ==> dataframe")
    var selectSQL=
      s"""
         |select uid,
         | case  hair when '长发'  then 101
         |            when '短发'  then 102
         |            when '板寸'  then 103   end hair,
         |  height,
         | case  skirt when '是'  then 11
         |            when '否'  then 10     end skirt,
         |  case age   when '80后'  then 80
         |             when '90后'  then 90
         |             when '00后'  then 100 end  age,
         |  gender
         |from user_profile2077.student
         |
       """.stripMargin

    val dataFrame: DataFrame = sparkSession.sql(selectSQL)

    // 2  把数据 拆分成 训练集 和 测试集  8:2 7:3
    println("2  把数据 拆分成 训练集 和 测试集  8:2 7:3")
    val   Array(trainDataFrame,testDataFrame) = dataFrame.randomSplit(Array(0.8,0.2))

    //3   初始化自定义流水线
    println("3   初始化自定义流水线")
    val myPipeline: MyPipeline = new MyPipeline().setLabelColName("gender")
      .setFeatureColName(Array("hair", "height", "skirt", "age"))
      .setMaxCategories(10)
      .setMaxBins(20)
      .setMaxDepth(3)
      .setMinInfoGain(0.1)
      .setMinInstancesPerNode(5)
      .init()


    //4   用流水线对象对训练集进行训练
    println("4   用流水线对象对训练集进行训练")
    myPipeline.train(trainDataFrame)
    myPipeline.printTree()
    myPipeline.printFeatureWeight()


    //5   用流水线对象的model 对测试集进行测试
    println("5   用流水线对象的model 对测试集进行测试")
    val predictedDataframe: DataFrame = myPipeline.predict(testDataFrame)



    //6 转换原值
    println("6  转换原值")
    val convertedDataframe: DataFrame = myPipeline.convertOrigin(predictedDataframe)

    convertedDataframe.show(1000,false)

    //7 评估
    println("7 评估")
    myPipeline.printEvaluate(predictedDataframe)

    //8 保存模型
    println("8  存储模型")
    val properties: Properties = MyPropertiesUtil.load("config.properties")
    val modelPath: String = properties.getProperty("model.path.student_gender")

    myPipeline.saveModel(modelPath)

  }

}
